package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entityJPA.User;
import entityJPA.Usertype;
import service.UserService;

/**
 * Servlet implementation class UpdateUserServiceServlet
 */
@WebServlet(name = "editUser", urlPatterns = { "/UpdateUserServiceServlet" })
public class UpdateUserServiceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	UserService userService;
	PrintWriter out;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateUserServiceServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		User user = userService.getUserById(Integer.parseInt(request
				.getParameter("id")));
		user.setFirstName(request.getParameter("firstName"));
		user.setLastName(request.getParameter("lastName"));
		//user.setPassword(request.getParameter("password"));
		user.setHomeNumber(request.getParameter("homeNumber"));
		user.setCellNumber(request.getParameter("cellNumber"));
		user.setEmail(request.getParameter("email"));
		user.setRank(request.getParameter("rank"));
		user.setDescription(request.getParameter("description"));
		user.setUsertype(userService.getUsertypeByTitle(request
				.getParameter("userType")));
		try {
			userService.editUser(user);
			out = response.getWriter();
			out.write("editare cu succes");
		} catch (Exception e) {
			out = response.getWriter();
			out.write("Eroare la editare");
		}

	}

}
